package com.qooco.watersystem.extensions;

import com.qooco.watersystem.extensions.client_handlers.FinishWaterHendler;

import com.qooco.watersystem.extensions.client_handlers.ReadyHandler;
import com.qooco.watersystem.extensions.client_handlers.RestartHandler;
import com.qooco.watersystem.extensions.client_handlers.StartWaterHandler;
import com.qooco.watersystem.extensions.client_handlers.StayWaterHandler;
import com.qooco.watersystem.extensions.server_handlers.OnSpectatorToPlayerHandler;
import com.qooco.watersystem.extensions.server_handlers.OnUserGoneHandler;
import com.smartfoxserver.v2.core.SFSEventType;
import com.smartfoxserver.v2.entities.Room;
import com.smartfoxserver.v2.entities.User;
import com.smartfoxserver.v2.entities.data.ISFSObject;
import com.smartfoxserver.v2.entities.data.SFSObject;
import com.smartfoxserver.v2.extensions.SFSExtension;

public class CocktailExtension extends SFSExtension 
{

	public final String version = "0.0.0.1";
	private Boolean isGameStarted;
	private LastGameEndResponse lastGameEndResponse;
	
	@Override
	public void init() 
	{
		trace("Cocktail game Extension for started, rel. " + version);
		
		isGameStarted = false;
		
	    addRequestHandler("start", StartWaterHandler.class);
	    addRequestHandler("stay", StayWaterHandler.class);
	    addRequestHandler("finish", FinishWaterHendler.class);
	    
	    addRequestHandler("restart", RestartHandler.class);
	    addRequestHandler("ready", ReadyHandler.class);
	    
	    addEventHandler(SFSEventType.USER_DISCONNECT, OnUserGoneHandler.class);
	    addEventHandler(SFSEventType.USER_LEAVE_ROOM, OnUserGoneHandler.class);
	    addEventHandler(SFSEventType.SPECTATOR_TO_PLAYER, OnSpectatorToPlayerHandler.class);
	}
	
	@Override
	public void destroy() 
	{
		super.destroy();
		trace("Cocktail game destroyed!");
	}
	
	boolean isGameStarted()
	{
		return isGameStarted;
	}
	
	public void startGame()
	{
		trace("in CocktailExtension.startGame() 002"); 
		
		//if (isGameStarted)
		//	throw new IllegalStateException("Game is already started!");
		
		trace("after throw new IllegalStateException");
		
		lastGameEndResponse = null;
		isGameStarted = true;
		
		trace("before getParentRoom()");
		
		Room r = getParentRoom();
		if (r == null) trace("CocktailExtension.startGame() parentRoom == null");
		
		User player1 = getParentRoom().getUserByPlayerId(1);
		if (player1 == null) trace("CocktailExtension.startGame() player1 == null");
		User player2 = getParentRoom().getUserByPlayerId(2);
		if (player2 == null) trace("CocktailExtension.startGame() player2 == null");
		
		// Send START event to client
		ISFSObject resObj = new SFSObject();
		resObj.putUtfString("p1n", player1.getName());
		resObj.putInt("p1i", player1.getId());
		resObj.putUtfString("p2n", player2.getName());
		resObj.putInt("p2i", player2.getId());
		
		trace("CocktailExtension.startGame() before send"); 
		send("start_g", resObj,  getParentRoom().getUserList());
		trace("CocktailExtension.startGame() after send");
	}
	
	void stopGame()
	{
		stopGame(false);
	}
	
	public void stopGame(boolean resetTurn)
	{
		isGameStarted = false;
	}
	
	public Room getGameRoom()
	{
		return this.getParentRoom();
	}
	
	public LastGameEndResponse getLastGameEndResponse()
    {
	    return lastGameEndResponse;
    }
	
	//void setLastGameEndResponse(LastGameEndResponse lastGameEndResponse)
    //{
	//    this.lastGameEndResponse = lastGameEndResponse;
    //}
	
	public void updateSpectator(User user)
	{
		ISFSObject resObj = new SFSObject();
		
		User player1 = getParentRoom().getUserByPlayerId(1);
		User player2 = getParentRoom().getUserByPlayerId(2);
		
		resObj.putBool("status", isGameStarted);
		
		if (player1 == null)
			resObj.putInt("p1i", 0); // <--- indicates no P1
		else
		{
			resObj.putInt("p1i", player1.getId());
			resObj.putUtfString("p1n", player1.getName());
		}
		
		if (player2 == null)
			resObj.putInt("p2i", 0); // <--- indicates no P2
		else
		{
			resObj.putInt("p2i", player2.getId());
			resObj.putUtfString("p2n", player2.getName());
			
		}
		
		send("specStatus", resObj, user);
	}

}
